//
//  RoyaViews.m
//  DS爱信
//
//  Created by 秦建英 on 14-6-9.
//  Copyright (c) 2014年 秦建英. All rights reserved.
//

#import "RoyaViews.h"
#import "DSDialViewController.h"
#import "CallHisViewController.h"
#import "AppDelegate.h"
#define CONFIGURE_BUTTON(BTN,X,Y,TITLE,TAG) {\
self.BTN = [UIButton buttonWithType:UIButtonTypeCustom];\
[self.BTN setFrame:CGRectMake(X, Y, widthOfButton, heightOfButton)];\
[self.BTN setShowsTouchWhenHighlighted:YES];\
[self.BTN setTintColor:[UIColor whiteColor]];\
[self.BTN setBackgroundColor:[UIColor blackColor]];\
[self.BTN setTag:TAG];\
[self.BTN addTarget:self action:@selector(onKeyPressed:) \
forControlEvents:UIControlEventTouchUpInside];\
[self addSubview:self.BTN];\
}
#define PULL_DOWN_OFFSET 5.0

#define TAG_KEY_DIAL     111

#define TAG_KEY_UNDO     444

@implementation RoyaViews
{
    NSUserDefaults *users;
    NSMutableData *_data;
    DSDialViewController *lg;
    NSMutableArray *_tellArray;
    AppDelegate *_app;
    NSString *_hj;
}
-(id)init
{
    _tellArray =[NSMutableArray array];
    _app =[UIApplication sharedApplication].delegate;
    CGRect frame = [[UIScreen mainScreen]applicationFrame];
    frame.size.height /= 1.1;
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat heightOfTextField = 30.0;
        CGFloat widthOfButtonOnOff = 40.0;
        self.txtNumber = [[UITextField alloc]initWithFrame:CGRectMake(30,0,self.frame.size.width - self.btnOffOn.frame.size.width-80,heightOfTextField)];
        [self.txtNumber setBorderStyle:UITextBorderStyleRoundedRect];
        [self.txtNumber setEnabled:NO];
        [self.txtNumber setPlaceholder:@"号码:  固话前请加拨区号"];
        self.txtNumber.text =@"";
        self.txtNumber.font=[UIFont fontWithName:@"Times New Roman" size:28];
        self.txtNumber.adjustsFontSizeToFitWidth=YES;
        self.txtNumber.backgroundColor =[UIColor colorWithRed:0.2f green:0.6f blue:0.9f alpha:1];
        self.txtNumber.delegate = self;
        [self addSubview:self.txtNumber];
        
        self.btnOffOn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.btnOffOn setFrame:CGRectMake(self.txtNumber.frame.size.width+25, 1, widthOfButtonOnOff+15, heightOfTextField)];
        [self.btnOffOn setShowsTouchWhenHighlighted:YES];
        [self.btnOffOn setImage:[UIImage imageNamed:@"btn_imemoji_del_down.png"] forState:UIControlStateNormal];
        [self.btnOffOn addTarget:self
                          action:@selector(onButtonOnOffPressed:)
                forControlEvents:UIControlEventTouchUpInside];
        self.btnOffOn.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        [self addSubview:self.btnOffOn];
        
        CGFloat heightOfButton = frame.size.height / 5.0 - heightOfTextField;
        CGFloat widthOfButton = frame.size.width / 3.0;
        //configure the number key
        CONFIGURE_BUTTON(btn1, 0, heightOfTextField, @"1",1);
        [self.btn1 setBackgroundImage:[UIImage imageNamed:@"1.png"]
                             forState:UIControlStateNormal];
        CONFIGURE_BUTTON(btn2, widthOfButton , heightOfTextField, @"2",2);
        [self.btn2 setBackgroundImage:[UIImage imageNamed:@"2.png"]
                             forState:UIControlStateNormal];
        CONFIGURE_BUTTON(btn3, widthOfButton*2, heightOfTextField, @"3",3);
        [self.btn3 setBackgroundImage:[UIImage imageNamed:@"3.png"]
                             forState:UIControlStateNormal];
        CONFIGURE_BUTTON(btn4, 0, heightOfButton + heightOfTextField, @"4",4);
        [self.btn4 setBackgroundImage:[UIImage imageNamed:@"4.png"]
                             forState:UIControlStateNormal];
        CONFIGURE_BUTTON(btn5, widthOfButton, heightOfButton + heightOfTextField, @"5",5);
        [self.btn5 setBackgroundImage:[UIImage imageNamed:@"5.png"]
                             forState:UIControlStateNormal];
        CONFIGURE_BUTTON(btn6, widthOfButton*2, heightOfButton + heightOfTextField, @"6",6);
        [self.btn6 setBackgroundImage:[UIImage imageNamed:@"6.png"]
                             forState:UIControlStateNormal];
        CONFIGURE_BUTTON(btn7, 0, heightOfButton*2 + heightOfTextField, @"7",7);
        [self.btn7 setBackgroundImage:[UIImage imageNamed:@"7.png"]
                             forState:UIControlStateNormal];
        CONFIGURE_BUTTON(btn8, widthOfButton, heightOfButton*2 + heightOfTextField, @"8",8);
        [self.btn8 setBackgroundImage:[UIImage imageNamed:@"8.png"]
                             forState:UIControlStateNormal];
        CONFIGURE_BUTTON(btn9, widthOfButton*2, heightOfButton*2 + heightOfTextField, @"9",9);
        [self.btn9 setBackgroundImage:[UIImage imageNamed:@"9.png"]
                             forState:UIControlStateNormal];
        CONFIGURE_BUTTON(btn0, widthOfButton, heightOfButton*3 + heightOfTextField, @"0",0);
        [self.btn0 setBackgroundImage:[UIImage imageNamed:@"0.png"]
                             forState:UIControlStateNormal];
        CONFIGURE_BUTTON(btnDial,0,heightOfButton*3 + heightOfTextField, @"Dial",TAG_KEY_DIAL);
        [self.btnDial  setBackgroundImage:[UIImage imageNamed:@"电话.png"]
                                 forState:UIControlStateNormal];
        CONFIGURE_BUTTON(btnUndo, widthOfButton*2, heightOfButton*3 + heightOfTextField, @"Undo",TAG_KEY_UNDO);
        [self.btnUndo setBackgroundImage:[UIImage imageNamed:@"删除.png"]
                                forState:UIControlStateNormal];
        mIsLayOn = NO;
        [self setLayOn:mIsLayOn];
        self.backgroundColor = [UIColor blackColor];
    }
    return self;
}
-(void)setLayOn:(BOOL)isLayOn
{
    CGFloat adjustOffset = (self.frame.size.height )/8.0 + PULL_DOWN_OFFSET;
    if (isLayOn == YES) {
        adjustOffset *= 1;
    }
}
-(void)handleCall:(NSString *) phoneNum
{
    sleep(0.8);
    if(phoneNum.length)
    {
        _hj=@"未知联系人";
        NSMutableArray *sdk =[[NSMutableArray alloc]initWithObjects:phoneNum,_hj,nil];
        lg =[[DSDialViewController alloc]init];
        lg.view=self;
        CallHisViewController *call =[[CallHisViewController alloc]init];
        call.tellName=sdk;
        NSUserDefaults *myUserDefaults = [NSUserDefaults standardUserDefaults];
        if([myUserDefaults objectForKey:@"tellDemol"])
        {
            _tellArray = [[NSMutableArray alloc] initWithArray:[myUserDefaults objectForKey:@"tellDemol"]];
            NSMutableDictionary *dict =[[NSMutableDictionary alloc]initWithCapacity:60];
            if(_app.rarray.count>0)
            {
                [dict setObject:phoneNum forKey:@"Demol"];
                [dict setObject:[_app.rarray objectAtIndex:0] forKey:@"Demolname"];
            }else
            {
                [dict setObject:phoneNum forKey:@"Demol"];
                [dict setObject:_hj forKey:@"Demolname"];
            }
            [_tellArray addObject:dict];
            [myUserDefaults setObject:_tellArray forKey:@"tellDemol"];
        }
        else
        {
            _tellArray= [[NSMutableArray alloc] initWithCapacity:60];
            NSMutableDictionary *dict =[[NSMutableDictionary alloc]initWithCapacity:60];
            ;
            if(_app.rarray.count>0)
            {
                [dict setObject:phoneNum forKey:@"Demol"];
                [dict setObject:[_app.rarray objectAtIndex:0] forKey:@"Demolname"];
            }else
            {
                [dict setObject:phoneNum forKey:@"Demol"];
                [dict setObject:_hj forKey:@"Demolname"];
            }
            [_tellArray addObject:dict];
            [myUserDefaults setObject:_tellArray forKey:@"tellDemol"];
        }
        [myUserDefaults synchronize];
        [lg presentViewController:call animated:YES completion:^{
        }];
    }
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}
-(void)onKeyPressed:(id)sender
{
    NSInteger tag = [sender tag];
    NSString *text = self.txtNumber.text;
    switch (tag) {
        case TAG_KEY_DIAL:
            [self handleCall:text];
            break;
        case TAG_KEY_UNDO:
            if (text.length) {
                self.txtNumber.text = @"";
                if(self.txtNumber.text.length==0)
                {
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"tex" object:self.txtNumber.text];
                }
            }
            break;
        default:
            self.txtNumber.text = [NSString stringWithFormat:@"%@%d",text,tag];
            if(self.txtNumber.text .length>0)
            {
                [[NSNotificationCenter defaultCenter]postNotificationName:@"textField" object:self.txtNumber.text];
            }
            
            break;
    }
}
-(void)onButtonOnOffPressed:(id)sender
{NSString *text = self.txtNumber.text;
    if (text.length) {
        self.txtNumber.text = [text stringByReplacingCharactersInRange:NSMakeRange(text.length-1, 1)withString:@""];
        if(self.txtNumber.text.length==0)
        {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"tex" object:self.txtNumber.text];
        }
    }
}
-(void)dealloc
{
    [self.btn0 release];
    [self.btn1 release];
    [self.btn2 release];
    [self.btn3 release];
    [self.btn4 release];
    [self.btn5 release];
    [self.btn6 release];
    [self.btn7 release];
    [self.btn8 release];
    [self.btn9 release];
    [self.btnDial release];
    [self.btnUndo release];
    [_app release];
    [super dealloc];
}
@end
